package net.dao.impl;

import java.util.List;

import net.dao.RectangleReferenceDao;
import net.model.RectangleReference;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository(value = "rectangleReferenceDao")
public class RectangleReferenceDaoImpl implements RectangleReferenceDao {

	@Autowired
	private SessionFactory sessionFactory;

	public RectangleReference getRectangleReference() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				RectangleReference.class);
		return (RectangleReference) criteria.list().get(0);
	}

	@Override
	public RectangleReference getRectangleReference(String nombreSistema) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				RectangleReference.class);
		List<RectangleReference> list = (List<RectangleReference>) criteria
				.add(Restrictions.eq("systemName", nombreSistema)).list();
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public void setRectangleReference(RectangleReference rectangleReference) {
		RectangleReference rr = getRectangleReference();
		rr.setWidth(rectangleReference.getWidth());
		rr.setHigh(rectangleReference.getHigh());
		rr.setLineThickness(rectangleReference.getLineThickness());
		sessionFactory.getCurrentSession().saveOrUpdate(rr);
	}

}
